package com.wsc.it.bridge.pull.service.impl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.wsc.it.bridge.pull.service.PullService;
import com.wsc.it.bridge.push.service.impl.PushServiceImpl;
import com.wsc.it.utils.HttpClientUtil;

@Service
@Component
public class PullServcieImpl implements PullService {
    private static final Logger log = LoggerFactory.getLogger(PushServiceImpl.class);
    //牵牛花商品通知
    @Value("${http.push.qnh.goodInfoNotice}")
    private String qnhGoodInfoNotice;

    @Value("${http.push.qnh.qnhImgNotice}")
    private String qnhImgNotice;

    @Override
    public void pullQnhGoodInfoNotice(HttpServletRequest request, HttpServletResponse response) {
        httpPost(request, response, qnhGoodInfoNotice);
    }

    @Override
    public void pullQnhImgNotice(HttpServletRequest request, HttpServletResponse response) {
        httpPost(request, response, qnhImgNotice);
    }

    static void httpPost(HttpServletRequest request, HttpServletResponse response, String url){
        try {
            String param = HttpClientUtil.parseHttpString(request);
            Map<String, String> paramUrl = HttpClientUtil.getAllRequestParam(request);
            log.info("请求参数paramUrl：{},paramBody:{}", paramUrl, param);
            String responseBody = HttpClientUtil.doPost(url, param);
            log.info("url:{},res={}", url, responseBody);
            response.setContentType("application/xml;charset=UTF-8");
            response.getWriter().write(responseBody);
        } catch (Exception e) {
            log.error("{} 调用接口异常：{}",url, e.getMessage());
        }
    }
}
